package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author solang
 * @date 2022-12-26 17:23
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增菜品同时保存菜品口味
     * 由于是多表操作,需要事务支持,保存事务一致性
     *
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品dish
        this.save(dishDto);
        //this.save后菜品id已经生成,获取菜品id存入菜品口味
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        //使用stream流遍历重新赋值,末尾加collect(Collectors.toList())返回list集合,否则返回的是stream(Stream<DishFlavor>)流
        flavors = flavors.stream().map((item) -> {
            //将菜品口味表中菜品id赋值
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味dishFlavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询回显
     * @param id
     * @return
     */

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //1.查询菜品信息
        Dish dish = this.getById(id);
        //2.查询菜品口味
        //添加条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        //3.加入DishDto
        DishDto dishDto = new DishDto();
        //拷贝
        BeanUtils.copyProperties(dish,dishDto);

        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 更新
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish
        this.updateById(dishDto);
        Long dishId = dishDto.getId();
        //先删除原先菜品口味
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //删除原先数据
        dishFlavorService.remove(queryWrapper);
        //添加新数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            //将菜品口味表中菜品id赋值
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void saveWithDish(Integer status, Long[] ids) {
        //直接删除菜品
        UpdateWrapper updateWrapper = new UpdateWrapper();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
//        queryWrapper.setEntity()

    }
}
